﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Tauren.Business.Entity.ValidationAttributes
{
    /// <summary>
    /// 验证邮箱
    /// </summary>
    public class EmailAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = ValidationResult.Success;

            if (!string.IsNullOrEmpty(value + ""))
            {
                ///邮箱
                Regex reg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
                if (!reg.IsMatch(value + ""))
                {
                    validationResult = new ValidationResult(ErrorMessage);
                }
            }
            return validationResult;
        }
    }
    /// <summary>
    /// 验证电话号码
    /// </summary>
    public class PhoneValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = ValidationResult.Success;

            if (!string.IsNullOrEmpty(value + ""))
            {
                Regex reg = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
                if (!reg.IsMatch(value + ""))
                {
                    validationResult = new ValidationResult(ErrorMessage);
                }
            }
            return validationResult;
        }
    }
    /// <summary>
    /// 验证年龄
    /// </summary>
    public class AgeAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = ValidationResult.Success;

            if (!string.IsNullOrEmpty(value + ""))
            {
                int age = (int)value;
                if (age > 120 || age < 0)
                {
                    validationResult = new ValidationResult(ErrorMessage);
                }
            }
            return validationResult;
        }
    }
    /// <summary>
    /// 验证年龄
    /// </summary>
    public class IDCardAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            ValidationResult validationResult = ValidationResult.Success;

            if (!string.IsNullOrEmpty(value + ""))
            {
                Regex reg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
                if (!reg.IsMatch(value + ""))
                {
                    validationResult = new ValidationResult(ErrorMessage);
                }
            }
            else
            {
                validationResult = new ValidationResult("身份证必填!");
            }
            return validationResult;
        }
    }
}
